package com.leetcode.q21;

/**
 * <p> description: desc</p>
 *
 * @author tanzc
 * @date 2022/3/30
 **/
public class MergeTwoLists {

    public static void main(String[] args) {
        MergeTwoLists mergeTwoLists = new MergeTwoLists();
    }

    public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
        ListNode list = new ListNode();
        ListNode q = list;
        ListNode l1 = list1, l2 = list2;
        while (list1 != null && list2 != null) {
            if (list1.val <= list2.val) {
                q.next = list1;
                q = q.next;
                list1 = list1.next;
            } else {
                q.next = list2;
                q = q.next;
                list2 = list2.next;
            }
        }
        if (list1 == null) {
            q.next = list2;
        } else {
            q.next = list1;
        }

        return list.next;
    }
}

class ListNode {
    int val;
    ListNode next;

    ListNode() {
    }

    ListNode(int val) {
        this.val = val;
    }

    ListNode(int val, ListNode next) {
        this.val = val;
        this.next = next;
    }
}